package com.sokal.easycare.common.utils.QRcoder;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Random;
import java.util.UUID;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.sokal.easycare.common.utils.FastDFS.FastDFSClient;

public class QRcoderUtil {
	public static void main(String[] args) {
		try {

			System.out.println(encode("中文", "/Users/liliangdong/Downloads/", true));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 生成二维码
	 * 
	 * @param encodeString
	 * @param codePath
	 * @return
	 * @throws Exception
	 */
	/**
	 * 
	 * @param encodeString
	 * @param codePath
	 * @param isToFileServer
	 *            false为不上传到文件服务器 true为要上传到文件服务器
	 * @return
	 * @throws Exception
	 */
	public static String encode(String encodeString, String codePath, Boolean isToFileServer) throws Exception {
		int qrcodeWidth = 300;
		int qrcodeHeight = 300;
		String qrcodeFormat = "png";
		HashMap<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>();
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		BitMatrix bitMatrix = new MultiFormatWriter().encode(encodeString, BarcodeFormat.QR_CODE, qrcodeWidth,
				qrcodeHeight, hints);
		BufferedImage image = new BufferedImage(qrcodeWidth, qrcodeHeight, BufferedImage.TYPE_INT_RGB);
		String fileName = UUID.randomUUID().toString().replaceAll("-", "") + "." + qrcodeFormat;
		File QrcodeFile = new File(codePath + fileName);
		ImageIO.write(image, qrcodeFormat, QrcodeFile);
		MatrixToImageWriter.writeToFile(bitMatrix, qrcodeFormat, QrcodeFile);
		if (isToFileServer) {
			// 将二维码上传到文件服务器中
			String path=FastDFSClient.uploadFile(QrcodeFile, fileName);
			// 文件上传到服务器后，删除本地文件
			QrcodeFile.delete();
			return path;
		} else {
			return QrcodeFile.getAbsolutePath();
		}

	}

	/**
	 * 解析二维码
	 * 
	 * @param filePath
	 * @return
	 */
	public static String decode(String filePath) {
		String retStr = "";
		if ("".equalsIgnoreCase(filePath) && filePath.length() == 0) {
			return "图片路径为空!";
		}
		try {
			BufferedImage bufferedImage = ImageIO.read(new FileInputStream(filePath));
			LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
			Binarizer binarizer = new HybridBinarizer(source);
			BinaryBitmap bitmap = new BinaryBitmap(binarizer);
			HashMap<DecodeHintType, Object> hintTypeObjectHashMap = new HashMap<>();
			hintTypeObjectHashMap.put(DecodeHintType.CHARACTER_SET, "UTF-8");
			Result result = new MultiFormatReader().decode(bitmap, hintTypeObjectHashMap);
			retStr = result.getText();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retStr;
	}

}
